<template>
	<view class="page">
		<checkbox-group @change="selected">
			<block v-for="dept in list" :key="dept.id">
				<view class="list-title">{{ dept.deptName }}（{{ dept.count }}人）</view>
				<view class="item" v-for="member in dept.members" :key="member.userId">
					<view class="key">{{ member.name }}</view>
					<checkbox class="value" :value="member.userId" :checked="member.checked"></checkbox>
				</view>
			</block>
		</checkbox-group>
	</view>
</template>

<script>
export default {
	data() {
		return {
			list: [],
			members: []
		};
	},
	onShow: function() {
		this.loadData(this);
	},
	onLoad: function(options) {
		if (options.hasOwnProperty('members')) {
			//传过来的路径有members的话，进行赋值
			let members = options.members;
			this.members = members.split(',');
		}
	},
	methods: {
		loadData: function(ref) {
			ref.ajax(ref.url.searchUserGroupByDept, 'POST', { keyword: ref.keyword }, function(resp) {
				let result = resp.data.result;
				ref.list = result;
				for (let dept of ref.list) {
					for (let member of dept.members) {
						//选中打勾
						if (ref.members.indexOf(member.userId + '') != -1) {
							member.checked = true;
						} else {
							member.checked = false;
						}
					}
				}
			});
		},
		selected: function(e) {
			let that = this;
			that.members = e.detail.value;
			//获取所有页面
			let pages = getCurrentPages();
			//获取上一级页面，就是会议详情页面
			let prevPage = pages[pages.length - 2];
			prevPage.members = that.members;
			prevPage.finishMembers = true;
		}
	}
};
</script>

<style lang="less">
	@import url("members.less");
</style>
